package com.course.design.socketio;

import com.corundumstudio.socketio.Configuration;
import com.corundumstudio.socketio.SocketConfig;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.annotation.SpringAnnotationScanner;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;

/**
 * 描述:
 *
 * @author qinlianji
 * @create 2020-02-18
 */
@org.springframework.context.annotation.Configuration
public class SocketIoConfig {

    @Value("${im.server.host}")
    private String host;

    @Value("${im.server.port}")
    private int port;

    @Value("${im.server.maxFramePayloadLength}")
    private int maxFramePayloadLength;

    @Value("${im.server.maxHttpContentLength}")
    private int maxHttpContentLength;

    @Value("${im.server.workCount}")
    private int workCount;

    @Value("${im.server.bossCount}")
    private int bossCount;

    @Value("${im.server.allowCustomRequests}")
    private Boolean allowCustomRequests;

    @Value("${im.server.upgradeTimeout}")
    private int upgradeTimeout;

    @Value("${im.server.pingTimeout}")
    private int pingTimeout;

    @Value("${im.server.pingInterval}")
    private int pingInterval;

    @Bean
    public SocketIOServer socketIOServer() {

        SocketConfig socketConfig = new SocketConfig();
        socketConfig.setTcpNoDelay(true);
        socketConfig.setSoLinger(0);
        Configuration config = new Configuration();
        config.setSocketConfig(socketConfig);
        config.setHostname(host);
        config.setPort(port);
        config.setBossThreads(bossCount);
        config.setWorkerThreads(workCount);
        config.setAllowCustomRequests(allowCustomRequests);
        config.setUpgradeTimeout(upgradeTimeout);
        config.setPingTimeout(pingTimeout);
        config.setPingInterval(pingInterval);
        return new SocketIOServer(config);
    }

    @Bean
    public SpringAnnotationScanner springAnnotationScanner(SocketIOServer socketServer) {
        return new SpringAnnotationScanner(socketServer);
    }


}
